package com.yun.zf.admin.controller;

import com.yun.zf.common.admin.model.house.SearchMo;
import com.yun.zf.common.utils.TreeNodes;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.service.ZfCityService;
import com.yun.zf.system.service.ZfSearchService;
import com.yun.zf.system.service.ZfSubwayStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/search")
@Api(value = "", tags = "搜索条件")
@Log
public class SearchController extends BaseController {

    @Autowired
    private ZfSearchService searchService;
    @Autowired
    private ZfCityService cityService;
    @Autowired
    private ZfSubwayStationService stationService;

    /**
     * 根据不同类型比如，
     *
     * @param type 小区，二手房(区域，地铁) 租房(区域，地铁) 查询搜索条件
     * @return
     */
    @GetMapping("/listSearch")
    @ApiOperation(value = "搜索列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型如sec_handler、community", required = true),
            @ApiImplicitParam(name = "areaOrSubway", value = "区域或者地铁：area、subway", required = false)})
    public Response listSearch(@RequestParam(required = true) String type, String areaOrSubway) {
        SearchMo searchMo = searchService.getSearchMo(type, aCode, areaOrSubway);
        List<TreeNodes> cityList = searchMo.getCityList();
        if (cityList != null && cityList.size() > 0) {
            for (TreeNodes t : cityList
            ) {
                Map<String, List<TreeNodes>> groupBy = t.getChildren().stream().collect(Collectors.groupingBy(TreeNodes::getFirstPinyin));
                t.setGroupChildren(groupBy);
                t.setChildren(null);
            }
        }

        return Response.success(searchMo);
    }


}
